home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / ConnectManager.java < prev    next >
Text File  |  1997-06-09  |  3KB  |  104 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. import java.util.*;
  18. import java.net.*;
  19. import java.io.*;
  20.  
  21. /**
  22.  * This class manages SNMP connections to various SNMP agents
  23.  * on the network.  A process can have several connections
  24.  * open simutaneously but it is limited in number.  Instead of opening
  25.  * and closing different connections every time a different SNMP device 
  26.  * is polled, a number of open connections can be cached. This class
  27.  * manages the list of open SNMP connections and will open new 
  28.  * connections and close old ones.
  29.  * <P>
  30.  * @version     $Id: ConnectManager.java,v 1.4 1997/05/18 08:11:53 alex Exp $
  31.  * @author      Alex Kowalenko
  32.  * @see Connect
  33.  * @see Pdu
  34.  */
  35.  
  36. public class ConnectManager {
  37.  
  38.     short _cacheSize;
  39.     int _timeOut;
  40.     Hashtable _cache; // Dictionary<String, Connect>
  41.  
  42. /**
  43.  * Creates a manager with a cache of cacheSize
  44.  */
  45.  
  46.     ConnectManager(short cacheSize) {
  47.     _cacheSize = cacheSize;
  48.     _cache = new Hashtable(cacheSize);
  49.     };
  50.   
  51. /**
  52.  * finalize
  53.  */
  54.  
  55.   protected void finalize() {
  56.       for(Enumeration e = _cache.elements(); e.hasMoreElements(); ) {
  57.       ((Connect)e.nextElement()).close();
  58.       };
  59.   };
  60.  
  61. /**
  62.  *  Set the lenght of timeouts for a SNMP response
  63.  */
  64.     
  65.     void setTimeOut(int seconds) {
  66.     _timeOut = seconds;
  67.     };
  68.   
  69. /**
  70.  * Send a PDU packet to the SNMP agent at this address.
  71.  * Return the SNMP agents reply as a PDU packet.
  72.  */
  73.  
  74.     PDU send(String address, PDU pdu) 
  75.     throws SnmpPDUException, SnmpNetException, SnmpTimeOutException,
  76.     SocketException, IOException, UnknownHostException {
  77.         if(!_cache.containsKey(address)) {
  78.         if(_cache.size() > _cacheSize) {    
  79.             // Close a connection.
  80.             String removeAddress = null;
  81.             int count = 0;
  82.             for(Enumeration iter = _cache.keys(); 
  83.             iter.hasMoreElements(); ) {
  84.             String item = ((String)iter.nextElement());
  85.             if(((Connect)_cache.get(item)).count() > count)
  86.                 removeAddress = item;
  87.             }
  88.             if(removeAddress != null)
  89.             ((Connect)_cache.get(removeAddress)).close();
  90.             _cache.remove(removeAddress);
  91.         }
  92.         Connect con = new Connect(address, 
  93.                       _timeOut, 
  94.                       Connect.SNMP_PORT);
  95.         con.open();
  96.         _cache.put(address,con);
  97.         }
  98.         ByteBuffer result = 
  99.         ((Connect)_cache.get(address)).send(pdu.BERSerialize());
  100.         return new PDU(result); // Parse returned buffer. 
  101.     }          
  102.  
  103. }
  104.